package com.ywy.controller.admin;

import com.ywy.entity.User;
import com.ywy.service.UserService;
import com.ywy.utils.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员用户Controller
 *
 * @author ywy
 * @date 2022/9/6 18:02
 */
@Controller
@RequestMapping("admin/user")
public class AdminUserController {
    @Resource
    private UserService userService;

    /**
     * 安全推出登录
     * @param session
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        SecurityUtils.getSubject().logout();
        return "redirect:/login.html";
    }

    /**
     * 跳转到用户充值积分页面
     *
     * @return
     */
    @RequestMapping("addPoints/{id}")
    public ModelAndView addPoints(@PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("id", id);
        mav.setViewName("admin/user/addPoints");
        return mav;
    }

    /**
     * 根据条件分页查询用户信息
     *
     * @param user
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions(value = {"USER_QUERY"})
    public Map<String, Object> list(User user, Integer page, Integer limit) {
        List<User> userList = userService.list(user, page, limit);
        Long total = userService.getTotal(user);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("count", total);
        resultMap.put("data", userList);
        return resultMap;
    }

    /**
     * 重置用户密码
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("resetPassword")
    @RequiresPermissions(value = {"RESET_PASSWORD"})
    public Map<String, Object> resetPassword(Integer id) {
        User oldUser = userService.getById(id);
        oldUser.setPassword(MD5Util.md5("123456"));
        userService.save(oldUser);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 用户积分充值
     *
     * @param id
     * @param points
     * @return
     */
    @ResponseBody
    @RequestMapping("addPoints")
    @RequiresPermissions(value = {"ADD_POINTS"})
    public Map<String, Object> addPoints(Integer id, Integer points) {
        User oldUser = userService.getById(id);
        oldUser.setPoints(oldUser.getPoints() + points);
        userService.save(oldUser);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 修改用户VIP状态
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("updateVipStatus")
    @RequiresPermissions(value = {"UPDATE_VIP_STATUS"})
    public Map<String, Object> updateVipState(User user) {
        User oldUser = userService.getById(user.getId());
        oldUser.setVip(user.isVip());
        userService.save(oldUser);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 修改用户状态
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("updateUserStatus")
    @RequiresPermissions(value = {"UPDATE_USER_STATUS"})
    public Map<String, Object> updateUserState(User user) {
        User oldUser = userService.getById(user.getId());
        oldUser.setStatus(user.isStatus());
        userService.save(oldUser);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }
}
